package com.chancingpack.service.impl;

import com.chancingpack.dal.model.WxUserLogin;
import com.chancingpack.service.

        WxUserLoginService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.chancingpack.dal.dao.WxUserLoginDao;
import com.chancingpack.util.CommonPage;
import com.chancingpack.util.CommonResult;
import com.chancingpack.util.ObjectUtil;
import com.chancingpack.util.VelocityUtil;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/7/17.
 */
@Service
public class WxUserLoginServiceImpl implements WxUserLoginService {
    @Autowired
    private WxUserLoginDao wxUserLoginDao;
    private static Logger logger = Logger.getLogger(WxUserLoginService.class);

    //添加用户
    public CommonResult insertRecord(WxUserLogin bean) {
        CommonResult result = new CommonResult();
        WxUserLogin record = null;

        try {
            if (this.wxUserLoginDao.getListByName(bean.getUsername())!=null){
                result.setResultMsg("用户名重复！");
            } else {
                record = (WxUserLogin) ObjectUtil.transfer(bean, WxUserLogin.class);
                result.setIsSuccess(this.wxUserLoginDao.insertRecord(record));
            }
        } catch (Exception e) {
            logger.error("添加角色异常", e);
        }
        return result;
    }

    public List<Map<String, Object>> getSgbasCustInfo() {
        return this.wxUserLoginDao.getSgbasCustInfo();
    }

    public List<Map<String, Object>> getRole() {
        return this.wxUserLoginDao.getRole();
    }

    //后台系统用户登陆验证
    public CommonResult getCheckLogin(WxUserLogin wxUserLogin) {
        CommonResult result = new CommonResult();
        int roleId=0;
        try {
            List<Map<String,Object>> UserLoginList=wxUserLoginDao.getCheckLogin(wxUserLogin);
            if (UserLoginList.size()>0){
                Object role_id= wxUserLoginDao.getCheckLogin(wxUserLogin).get(0).get("role_id");
                roleId=Integer.parseInt(role_id.toString());
            }

            if (this.wxUserLoginDao.getCheckLogin(wxUserLogin).size()>0 && (roleId==1 || roleId==2)){
                result.setIsSuccess(true);
            } else {
                result.setResultMsg("用户名或密码错误！");
            }
        } catch (Exception e) {
            logger.error("用户登陆异常", e);
        }
        return result;
    }

    //微信端用户登陆验证
    public CommonResult getWxCheckLogin(WxUserLogin wxUserLogin) {
        CommonResult result = new CommonResult();
        int roleId=0;
        try {
            List<Map<String,Object>> wxUserLoginList=wxUserLoginDao.getCheckLogin(wxUserLogin);
            if (wxUserLoginList.size()>0){
                Object role_id= wxUserLoginDao.getCheckLogin(wxUserLogin).get(0).get("role_id");
                roleId=Integer.parseInt(role_id.toString());
            }
            if (this.wxUserLoginDao.getCheckLogin(wxUserLogin).size()>0 && roleId==3){
                result.setIsSuccess(true);
            } else {
                result.setResultMsg("用户名或密码错误！");
            }
        } catch (Exception e) {
            logger.error("用户登陆异常", e);
        }
        return result;
    }

    public List<Map<String, Object>> getSgbasCustInfoByCustId(String custId) {
        return wxUserLoginDao.getSgbasCustInfoByCustId(custId);
    }


    //修改用户
    public CommonResult updateRecord(WxUserLogin bean) {
        CommonResult result = new CommonResult();
        WxUserLogin record = null;
        try {
            if (this.wxUserLoginDao.getListByName(bean.getUsername())!=null &&
                    wxUserLoginDao.getListByName(bean.getUsername()).getUserloginId()!=bean.getUserloginId()){
                result.setResultMsg("用户名重复！");
            } else {
                record = (WxUserLogin) ObjectUtil.transfer(bean, WxUserLogin.class);
                result.setIsSuccess(this.wxUserLoginDao.updateRecord(record));
            }
        } catch (Exception e) {
            logger.error("添加用户名异常", e);
        }
        return result;
    }

    //删除用户
    public CommonResult deleteRecord(Integer userloginId) {
        //return this.wxUserLoginDao.deleteRecord(userloginId);
        CommonResult result = new CommonResult();
        try {
                result.setIsSuccess(this.wxUserLoginDao.deleteRecord(userloginId));
        } catch (Exception e) {
            logger.error("删除角色异常", e);
        }
        return result;
    }

    public WxUserLogin getListByName(String username) {
        return this.wxUserLoginDao.getListByName(username);
    }

    public List<Map<String, Object>> getListMap(Integer userloginId) {
        return this.wxUserLoginDao.getListMap(userloginId);
    }

    public List<WxUserLogin> getList() {
        return this.wxUserLoginDao.getLastRecord();
    }

    //分页查询用户信息
    public CommonResult getListByPage(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult result = new CommonResult();
        try {
            Map<String,Object> mapsend = new HashMap<String, Object>();
            int curr=commonPage.getCurr();
            int pageSize =commonPage.getPageSize();
            mapsend = commonPage.getParamMap();
            mapsend.put("pagesize",curr*pageSize);
            mapsend.put("num",(curr-1)*pageSize);

            List<Map<String,Object>> wxUserLoginListMap=wxUserLoginDao.selectUserpage(mapsend);
            List<Map<String,Object>> wxUserLoginMap =new ArrayList<Map<String, Object>>();
            if(wxUserLoginDao.selectCount(mapsend) % pageSize>0) {
                commonPage.setPages(wxUserLoginDao.selectCount(mapsend) / pageSize+1);
            }else {
                commonPage.setPages(wxUserLoginDao.selectCount(mapsend) / pageSize);
            }
            for (int i=0;i<wxUserLoginListMap.size();i++){
                Map<String,Object> data=new HashMap<String,Object>();
                data.put("userloginId",wxUserLoginListMap.get(i).get("userlogin_id"));
                data.put("username",wxUserLoginListMap.get(i).get("username"));
                data.put("password",wxUserLoginListMap.get(i).get("password"));
                data.put("custId",wxUserLoginListMap.get(i).get("cust_id"));
                data.put("roleId",wxUserLoginListMap.get(i).get("role_id"));

                data.put("custName",wxUserLoginListMap.get(i).get("cust_name"));
                data.put("roleName",wxUserLoginListMap.get(i).get("role_name"));

                if(curr==1) {
                    data.put("viewId", i + 1);
                }else{
                    data.put("viewId",i+1+((curr-1)*15));
                }
                wxUserLoginMap.add(data);
            }
            commonPage.setPageData(wxUserLoginMap);
            Map<String, Object> showMap = new HashMap<String, Object>();
            showMap.put("wxUserLoginList", commonPage.getPageData());
            commonPage.setHtml(VelocityUtil.getHtmlByMap("wxUserLogin/wxUserLoginListData.vm", showMap,request,response,request.getSession().getServletContext()));
            result.setIsSuccess(true);
        } catch (Exception e) {
            logger.error("查询异常", e);
        }
        return result;
    }
}
